﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using App1AppHandler;

namespace Reborn_WorldServer.App1AppHandler
{
    public class Recipie : IComparable<Recipie>
    {
        public int ID;
        public string Name;
        public string Skill;
        public int ReqLevel;

        public List<RecipieItem> requiredItems = new List<RecipieItem>();
        public List<RecipieItem> yeildsItems = new List<RecipieItem>();

        virtual public void copyFrom(Recipie copyFromRecipie)
        {
            ID = copyFromRecipie.ID;
            Name = copyFromRecipie.Name;
            Skill = copyFromRecipie.Skill;
            ReqLevel = copyFromRecipie.ReqLevel;

            requiredItems = copyFromRecipie.requiredItems;
            yeildsItems = copyFromRecipie.yeildsItems;
        }

        public int CompareTo(Recipie other)
        {
            if (ReqLevel < other.ReqLevel)
                return 1;
            if (ReqLevel > other.ReqLevel)
                return -1;

            return 0;
        }

        virtual public void writeXmlAttributes(XmlTextWriter xmldoc, Character sendingToCharacter)
        {
            xmldoc.WriteAttributeString("ID", ID.ToString());
            xmldoc.WriteAttributeString("Name", Name.ToString());
            xmldoc.WriteAttributeString("Skill", Skill.ToString());
            xmldoc.WriteAttributeString("ReqLevel", ReqLevel.ToString());
        }
        virtual public void writeXmlElements(XmlTextWriter xmldoc, Character sendingToCharacter)
        {
            xmldoc.WriteStartElement("requiredItems");
            foreach (RecipieItem ri in requiredItems)
                xmldoc.WriteRaw(ri.toXml(sendingToCharacter));
            xmldoc.WriteEndElement();


            xmldoc.WriteStartElement("yeildsItems");
            foreach (RecipieItem ri in yeildsItems)
                xmldoc.WriteRaw(ri.toXml(sendingToCharacter));

            xmldoc.WriteEndElement();
        }

        public string toXml(Character sendingToCharacter)
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("recipie");

            writeXmlAttributes(xmldoc, sendingToCharacter);
            writeXmlElements(xmldoc, sendingToCharacter);

            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();

            stringWriter.Flush();

            result = stringWriter.ToString();

            return result;
        }
    }
}
